package com.sojson.udp.util;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.sojson.udp.util.bean.UdpConnect;

import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;

/**
 * Udp连接池工具类
 * 
 * @author liu
 * @date 2024-05-31
 */
@Slf4j
public class UdpPoolUtil {

    /** 参数名 */
    public static final String ADDR_KEY = "ADDR";
    private static ConcurrentMap<String, UdpConnect> map = new ConcurrentHashMap<>();

    public static void put(String tokenId, UdpConnect connect) {
        log.info(new StringBuilder().append("====== UdpOnOpen: ").append(tokenId).append(" ======").toString());
        connect.getCtx().channel().attr(AttributeKey.valueOf(UdpPoolUtil.ADDR_KEY)).set(tokenId);
        map.put(tokenId, connect);
    }

    public static UdpConnect get(String tokenId) {
        return map.get(tokenId);
    }

    public static void remove(String tokenId) {
        log.info(new StringBuilder().append("====== UdpOnClose: ").append(tokenId).append(" ======").toString());
        map.remove(tokenId);
    }

    public static Collection<UdpConnect> getValues() {
        return map.values();
    }

}